package com.ghsc.ghxc.jsb.ui.live.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.ghsc.ghxc.jsb.databinding.ItemVideoPlayAdBinding
import com.ghsc.ghxc.jsb.databinding.VideoListPlayItemBinding
import com.ghsc.ghxc.jsb.ui.live.bean.VideoDataBean
import com.ghsc.ghxc.jsb.ui.live.view.video.viewHolder.VideoAdHolder
import com.ghsc.ghxc.jsb.ui.live.view.video.viewHolder.VideoPlayPagerHolder

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.live.adapter
 * 类名：VideoPlayPagerAdapter
 * 创建时间：2024/5/10 11:19
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class VideoPlayPagerAdapter (private val mContext: Context, val playerTag:String) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val typeAd = 2
    private val typeVideo = 1

    val dataList by lazy { mutableListOf<VideoDataBean>() }
    private var onMyItemChildClickListener: ((view: View?, position: Int) -> Unit)? = null
    private var onVideoPlayListener: ((position: Int, dataInfo: VideoDataBean?) -> Unit)? = null

    fun setOnItemChildClickListener(listener: (view: View?, position: Int) -> Unit) {
        onMyItemChildClickListener = listener
    }

    fun setVideoPlayListener(listener: ((position: Int, dataInfo: VideoDataBean?) -> Unit)?) {
        onVideoPlayListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            typeAd -> {
                val itemBinding =
                    ItemVideoPlayAdBinding.inflate(LayoutInflater.from(mContext), parent, false)
                VideoAdHolder(itemBinding)
            }
            else -> {
                val itemBinding =
                    VideoListPlayItemBinding.inflate(LayoutInflater.from(mContext), parent, false)
                VideoPlayPagerHolder(playerTag, itemBinding)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val itemData = dataList[position]
        if (holder is VideoPlayPagerHolder) {
            holder.onBind(position, itemData)
            holder.setOnClickListener(onMyItemChildClickListener)
            holder.setOnVideoPlayListener(onVideoPlayListener)
            return
        }
        if (holder is VideoAdHolder) {
            holder.onBind(position, itemData)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun getItemViewType(position: Int): Int {
        return if (dataList.getOrNull(position)?.adView != null) typeAd else typeVideo
    }

    fun getIndexByArticleCode(articleCode: String?): Int {
        return dataList.indexOfFirst { it.vid == articleCode }
    }

    fun getDataLastIndex(): Int {
        return dataList.lastIndex
    }

    fun getItemOrNull(index: Int): VideoDataBean? {
        return dataList.getOrNull(index)
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setNewInstance(list: MutableList<VideoDataBean>?) {
        dataList.clear()
        dataList.addAll(list ?: emptyList())
        notifyDataSetChanged()
    }

    fun addData(list: MutableList<VideoDataBean>?) {
        if (list.isNullOrEmpty()) {
            return
        }
        dataList.addAll(list)
        notifyItemRangeInserted(dataList.size - list.size, list.size)
    }
}